﻿using System;
using EmailJabber.EmailJabber.Core.Entities;
using EmailJabber.Logic.Commands.Base;
using EmailJabber.Logic.Logic.BusinessLogic;

namespace EmailJabber.Logic.Commands
{
    public class GetLastCommand:CommandBase
    {

        protected override string GetDecription()
        {
            return "Return last log entry";
        }

        protected override Guid GetId()
        {
            return new Guid("9EA79705-4C62-4F49-9A9F-50981D665D0A");
        }


        protected override void ExecuteSafe(MessageIM comnd)
        {
            int lastMinutes = 40;
            int.TryParse(comnd.Body.Trim(), out lastMinutes);
            var log = DAL.DAL.Instance.GetLast(lastMinutes);

            MessageIM mess = new MessageIM(comnd.To,string.Format("Period last {0} minutes.", lastMinutes),comnd.From);
            BL.Instance.Send(mess);
            foreach (LogEntry logEntry in log)
            {
            	mess = new MessageIM(comnd.To,"Time:" + logEntry.Time + Environment.NewLine + logEntry.Extra,comnd.From);
                BL.Instance.Send(mess);
            }
        }
    }
}